#!/bin/bash
# 作者：院长
# 定义输出文件
REPORT_FILE="system_inspection_$(date +%Y%m%d%H%M).html"

# 开始生成HTML报告
cat > $REPORT_FILE << EOF
<html>
<head>
<title>院长NUC服务器巡检报告</title>
<style>
  body { font-family: Arial, sans-serif; margin: 30px; }
  h2 { color: #2c3e50; border-bottom: 2px solid #2c3e50; }
  table { border-collapse: collapse; width: 100%; margin-bottom: 20px; }
  th, td { border: 1px solid #dddddd; padding: 12px; text-align: left; }
  th { background-color: #f8f9fa; }
  .warning { background-color: #fff3cd; }
  .critical { background-color: #f8d7da; }
  .ok { background-color: #d4edda; }
</style>
</head>
<body>
<h1>院长NUC服务器巡检报告</h1>
<p>生成时间: $(date "+%Y-%m-%d %H:%M:%S")</p>
EOF

# 函数：添加报告段落
add_section() {
  echo "<h2>$1</h2>" >> $REPORT_FILE
  echo "<table>" >> $REPORT_FILE
}

# 函数：结束段落
end_section() {
  echo "</table>" >> $REPORT_FILE
}

# 函数：添加表格行
add_row() {
  echo "<tr><th>$1</th><td>$2</td><td>$3</td></tr>" >> $REPORT_FILE
}

# 主机基本信息
add_section "主机基本信息"
{
  echo "<tr><th>检查项</th><th>值</th><th>状态</th></tr>"

  # 操作系统信息
  os_name=$(grep PRETTY_NAME /etc/os-release | cut -d= -f2 | tr -d '"')
  add_row "操作系统" "$os_name" "正常"

  # 内核版本
  kernel_version=$(uname -r)
  add_row "内核版本" "$kernel_version" "正常"

  # 主机运行时间
  uptime=$(uptime -p)
  add_row "运行时间" "$uptime" "正常"

  # 主机名
  hostname=$(hostname -f)
  add_row "主机名" "$hostname" "正常"
} >> $REPORT_FILE
end_section

# 硬件信息
add_section "硬件信息"
{
  echo "<tr><th>检查项</th><th>值</th><th>状态</th></tr>"

  # CPU信息
  cpu_model=$(grep "model name" /proc/cpuinfo | uniq | cut -d: -f2 | sed 's/^ //')
  cpu_cores=$(grep -c "processor" /proc/cpuinfo)
  add_row "CPU型号" "$cpu_model" "正常"
  add_row "CPU核心数" "$cpu_cores" "正常"

  # 内存信息
  mem_total=$(free -h | awk '/Mem/{print $2}')
  add_row "总内存" "$mem_total" "正常"

  # 磁盘信息
  disk_info=$(df -hT / | awk 'NR==2{print $2,$3"/"$4}')
  add_row "根分区使用" "$disk_info" "正常"

} >> $REPORT_FILE
end_section

# 性能指标
add_section "性能指标"
{
  echo "<tr><th>检查项</th><th>值</th><th>状态</th></tr>"

  # 内存使用率
  mem_usage=$(free | awk '/Mem/{printf "%.1f%", $3/$2*100}')
  add_row "内存使用率" "$mem_usage" "正常"

  # CPU负载
  load_avg=$(uptime | awk -F'load average: ' '{print $2}')
  add_row "15分钟负载" "$load_avg" "正常"

  # 磁盘使用率
  disk_usage=$(df -h / | awk 'NR==2{print $5}')
  usage_percent=${disk_usage%\%}
  if [ $usage_percent -gt 90 ]; then
    status="critical"
  elif [ $usage_percent -gt 80 ]; then
    status="warning"
  else
    status="ok"
  fi
  add_row "根分区使用率" "$disk_usage" "<div class='$status'>$status</div>"

} >> $REPORT_FILE
end_section

# 安全配置检查
add_section "安全配置检查"
{
  echo "<tr><th>检查项</th><th>值</th><th>状态</th></tr>"

  # SSH配置检查
  ssh_root_login=$(grep "^PermitRootLogin" /etc/ssh/sshd_config | awk '{print $2}')
  if [ "$ssh_root_login" == "yes" ]; then
    status="warning"
  else
    status="ok"
  fi
  add_row "SSH Root登录" "$ssh_root_login" "<div class='$status'>$status</div>"

  # 防火墙状态
  firewall_status=$(systemctl is-active firewalld 2>/dev/null || echo "inactive")
  if [ "$firewall_status" == "active" ]; then
    status="ok"
  else
    status="warning"
  fi
  add_row "防火墙状态" "$firewall_status" "<div class='$status'>$status</div>"

} >> $REPORT_FILE
end_section

# 结束HTML报告
cat >> $REPORT_FILE << EOF
</body>
</html>
EOF

echo "巡检报告已生成: $REPORT_FILE"